﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using GuessLAB.UI.Classes;
using GuessLAB.Domain;
using System.Utility.UI;
using System.Utility.Messaging;
using System.Utility;
using System.Web.Security;

namespace GuessLAB.UI
{

    public partial class loginpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string pageurl = Constant.LoginUrl;
            try
            {
                Logger.LogMessage("fetching user info", LogLevel.DEBUG, LogMessageType.WebMessage);
                AppUser u = AppUser.GetUser(username.Value, password.Value);
                Logger.LogMessage("after fetching user info", LogLevel.DEBUG, LogMessageType.WebMessage);
                if (u == null)
                {
                    Logger.LogMessage("throwing messsage", LogLevel.DEBUG, LogMessageType.WebMessage);
                    PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
                }

                else
                {
                    StateManager<AppUser>.SetSessionState(SessionKey.UserInfo, u);
                    FormsAuthentication.SetAuthCookie(u.FirstName +" "+u.LastName, createPersistentCookie:false);
                    switch (u.Type)
                    {
                        case AppUserType.Admin:
                            pageurl = "Home.aspx";
                            break;
                    }
                    Response.Redirect(pageurl);
                }
            }
            catch (Exception ex)
            {
                errorMsg.Text = ex.Message;
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }
        }


       
    }
}